﻿using System;
using System.Text;

namespace Bpms.Common
{
    /// <summary>
    /// 时间格式操作类
    /// </summary>
    public class TimeFormat
    {
        /// <summary>
        /// DateTime类型的时间格式到 time_t的转换
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns>返回time_t，1970以来的秒数，最大(2038:2:18)</returns>
        public static long DateTime2Time_T(DateTime dateTime)
        {
            DateTime startTime = new DateTime(1970, 1, 1);
            TimeSpan span = dateTime - startTime;
            long time_t = (long)(span - TimeZoneInfo.Local.BaseUtcOffset).TotalSeconds;
            return time_t;
        }

        /// <summary>
        /// time_t 到 DateTime类的转换
        /// </summary>
        /// <param name="time_t"></param>
        /// <returns>返回DateTime类型时间</returns>
        public static DateTime Time_T2DateTime(long time_t)
        {
            DateTime dateTime = new DateTime(1970, 1, 1).AddSeconds(time_t).AddSeconds(TimeZoneInfo.Local.BaseUtcOffset.TotalSeconds);
            return dateTime;
        }


    }
}
